<!DOCTYPE html>
<html>
<head>
      <meta charset="UTF-8">
      <title>web3demo</title>
      <script src="web3.js"></script>
      
</head>
<body>
    <div class="hello">
      <script>
         //abi代码定义-从合约中拷贝而来
         abi = [
	{
		"constant": true,
		"inputs": [],
		"name": "getData",
		"outputs": [
			{
				"name": "",
				"type": "string"
			}
		],
		"payable": false,
		"stateMutability": "view",
		"type": "function"
	},
	{
		"constant": false,
		"inputs": [
			{
				"name": "_inData",
				"type": "string"
			}
		],
		"name": "setData",
		"outputs": [],
		"payable": false,
		"stateMutability": "nonpayable",
		"type": "function"
	}
]
      </script>
      <script>
         //1. 创建web3对象
         var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
         
         //查看账户信息-测试web3对象是否连接geth成功
         web3.eth.getAccounts(function(err,result){
            console.log(result)
         });
         
         //合约地址-创建合约时获得
         address = "0xcdf06ae7b925a34970b0f9b42e4a9389b3cefd94"
         //2. 通过合约地址以及合约的abi获得合约实例
         instance = new web3.eth.Contract(abi, address);
         //3. 调用合约中的方法
         instance.methods.getData().call({},function(e,r){
            console.log(e,r)
            document.write("<h1>"+r+"</h1>")
         });
      </script>  
    </div>  
</body>
</html>